package com.wx.mall.operate.service.impl;

import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.github.pagehelper.PageHelper;
import com.sun.xml.internal.ws.api.message.ExceptionHasMessage;
import com.wx.mall.operate.dto.RebDisDto;
import com.wx.mall.operate.dto.RebateDto;
import com.wx.mall.operate.dto.RebateSaveDto;
import com.wx.mall.operate.entity.*;
import com.wx.mall.operate.mapper.RebDiscountMapper;
import com.wx.mall.operate.mapper.RebateMapper;
import com.wx.mall.operate.service.*;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.wx.mall.operate.vo.RebToDisVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author j
 * @since 2020-06-01
 */
@Service
public class RebateServiceImpl extends ServiceImpl<RebateMapper, Rebate> implements IRebateService {

    @Autowired
    IRebDiscountService iRebDiscountService;

    @Autowired
    IRebLinkService iRebLinkService;

    @Autowired
    RebLinkServiceImpl rebLinkService;

    @Autowired
    IRebBraService iRebBraService;

    @Autowired
    IRebMiddleService iRebMiddleService;


    @Override
    public List<Rebate> findAllRebate(RebateDto dto) {
        PageHelper.startPage(dto.getPageNum(),dto.getPageSize());
        QueryWrapper<Rebate> queryWrapper=new QueryWrapper<>();
        if (dto.getSort()==0){
            queryWrapper.orderByAsc("start_time");
        };
        if (dto.getSort()==1){
            queryWrapper.orderByAsc("end_time");
        };
        if (dto.getSort()==2){
            queryWrapper.orderByAsc("activity_state");
        }
        List<Rebate> list=this.list(queryWrapper);
        return list;
    }

    @Override
    public List<Rebate> findScreenList(RebateDto dto) {
        PageHelper.startPage(dto.getPageNum(),dto.getPageSize());
        QueryWrapper<Rebate> queryWrapper=new QueryWrapper<>();
        if (dto.getTitle()!=null){
            queryWrapper.like("title",dto.getTitle());
        };
        if (dto.getStartTime()!=null){
            Calendar calendar=Calendar.getInstance();
            calendar.setTime(dto.getStartTime());
            calendar.add(Calendar.DAY_OF_MONTH,1);
            Date date=calendar.getTime();
            System.out.println(date);
            queryWrapper.le("start_time",date);
        };
        if (dto.getSort()==0){
            queryWrapper.orderByAsc("start_time");
        };
        if (dto.getSort()==1){
            queryWrapper.orderByAsc("end_time");
        };
        if (dto.getSort()==2){
            queryWrapper.orderByAsc("activity_state");
        };
        List<Rebate> list=this.list(queryWrapper);
        return list;
    }

    @Override
    public int findDisList(RebDisDto dto) {
        QueryWrapper<Rebate> queryWrapper=new QueryWrapper<>();
        queryWrapper.eq("id",dto.getId());
        List<Rebate> list1 = this.list(queryWrapper);
        int aa=0;
        for (Rebate reb:list1
             ) {
            reb.setTitle(dto.getTitle());
            reb.setPutType(dto.getPutType());
            reb.setDiscountId(dto.getDiscountId());
            reb.setActivityState(dto.getActivityState());
            reb.setEndTime(dto.getEndTime());
            reb.setStartTime(dto.getStartTime());
            if (dto.getCategory()!=reb.getCategory()){
                if (reb.getCategory().equals("type")){
                    rebLinkService.deleteSort(dto.getId());
                }else if (reb.getCategory().equals("goods")){
                    iRebMiddleService.deleteMid(dto.getId());
                }else if (reb.getCategory().equals("brands")){
                    iRebBraService.deleteBra(dto.getId());
                }
            }
            reb.setCategory(dto.getCategory());
            boolean b = this.updateById(reb);
            if (b==true){
                aa=1;
            }else {
                aa=0;
            }
        }
        return aa;
    }

    @Override
    public int saveRebate(List<RebDiscount> rebDiscounts, RebateSaveDto dto,
                          List<Long> ids,List<RebGoods> rebGoods, List<RebBrand> rebBras) {
        Random random=new Random();
        int a=random.nextInt(3)+1;
        int kk=1;
            Rebate rebate=new Rebate();
            rebate.setActivityState(a);
            rebate.setCategory(dto.getCategory());
            rebate.setDiscountId(dto.getDiscountId());
            rebate.setEndTime(dto.getEndTime());
            rebate.setPutType(dto.getPutType());
            rebate.setStartTime(dto.getStartTime());
            rebate.setTitle(dto.getTitle());
            boolean save = this.save(rebate);
            if (save==true){
                Long id=rebate.getId();
                for (RebDiscount dis : rebDiscounts
                        ) {
                    RebDiscount discount=new RebDiscount();
                    discount.setLevel(dis.getLevel());
                    discount.setMode(dis.getMode());
                    discount.setThreshold(dis.getThreshold());
                    discount.setRebateId(id);
                    boolean save1 = iRebDiscountService.save(discount);
                    if (save1==true){
                        kk=1;
                    }else {
                        kk=0;
                    }
                }
                if (rebate.getCategory().equals("type")){
                    for (Long idx:ids
                         ) {
                        RebLink rebLink=new RebLink();
                        rebLink.setRebateId(id);
                        rebLink.setSortId(idx);
                        boolean save1 = iRebLinkService.save(rebLink);
                        if (save1==true){
                            kk=1;
                        }else {
                            kk=0;
                        }
                    }
                }else if (rebate.getCategory().equals("goods")){
                    for (RebGoods rg:rebGoods
                         ) {
                        RebMiddle rebMiddle=new RebMiddle();
                        rebMiddle.setSortId(rg.getSortId());
                        rebMiddle.setRebateId(id);
                        rebMiddle.setGoodsId(rg.getId());
                        rebMiddle.setBrandId(rg.getBrandId());
                        boolean save1 = iRebMiddleService.save(rebMiddle);
                        if (save1==true){
                            kk=1;
                        }else {
                            kk=0;
                        }
                    }
                }else if (rebate.getCategory().equals("brands")){
                    for (RebBrand re:rebBras
                    ) {
                        RebBra rebBra=new RebBra();
                        rebBra.setSortId(re.getSortId());
                        rebBra.setRebateId(id);
                        rebBra.setBrandId(re.getId());
                        boolean save1 = iRebBraService.save(rebBra);
                        if (save1==true){
                            kk=1;
                        }else {
                            kk=0;
                        }
                    }
                }
            }
        return kk;
    }

    @Override
    public int deleteRebate(Long ids) {
        QueryWrapper<Rebate> queryWrapper=new QueryWrapper<>();
        queryWrapper.eq("id",ids);
        List<Rebate> list = this.list(queryWrapper);
        int aa=1;
        for (Rebate reb : list
                ) {
            int i = iRebDiscountService.deleteDiscount(reb.getId());
            boolean b = this.removeById(reb.getId());
            if (b==true){
                aa=1;
            }else {
                aa=0;
            }
            if (i==1){
                if (reb.getCategory().equals("type")){
                    int i1 = iRebLinkService.deleteSort(reb.getId());
                    if (i1==1){
                        aa=1;
                    }else {
                        aa=0;
                    }
                }else if (reb.getCategory().equals("goods")){
                    int i1 = iRebMiddleService.deleteMid(reb.getId());
                    if (i1==1){
                        aa=1;
                    }else {
                        aa=0;
                    }
                }else if (reb.getCategory().equals("brands")){
                    int i1 = iRebBraService.deleteBra(reb.getId());
                    if (i1==1){
                        aa=1;
                    }else {
                        aa=0;
                    }
                }
            }
        }
        return aa;
    }

}
